# -*- coding:utf-8 -*-
from cassandra.cluster import Cluster
from cassandra import ConsistencyLevel
from datetime import *
import json
import os

from wmcloud_dataclient import Client

DEFAULT_DEDATE = '1970-01-01'
def get_forward_basic_info(raw_msg):
    try:
        forward_basic_info = json.loads(raw_msg)
        if forward_basic_info['retCode'] == 1 and forward_basic_info['retMsg'] == 'Success':
            forward_info_d_list = forward_basic_info['data']
            cluster = Cluster(protocol_version=4)
            session = cluster.connect()
            session.set_keyspace('wmcloud')
            insert_found = session.prepare('insert into wmcloud.forward_basic_info(ticker, listdate, lasttradedate, firstdelidate, lastdelidate, secid, secfullname, secshortname, exchangecd, contmultnum, contmultunit, contractobject, contractstatus, contracttype, delicomminum, delicommiunit, deligrade, delimethod, delimonth, delipricemethod, deliyear, limitdownnum, limitdownunit, limitupnum, limitupunit, listbasisprice, minchgpricenum, minchgpriceunit, priceunit, pricevaliddecimal, settpricemethod, tradecomminum, tradecommiunit, trademarginratio, transcurrcd) values (?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?)')
            for data in forward_info_d_list:
                listdate_timestamp = datetime.strptime(data['listDate'], '%Y-%m-%d') + timedelta(hours=16) if 'listDate' in data else datetime.strptime(DEFAULT_DEDATE, '%Y-%m-%d')
                lasttradedate_timestamp = datetime.strptime(data['lastTradeDate'], '%Y-%m-%d') + timedelta(hours=16) if 'lastTradeDate' in data else datetime.strptime(DEFAULT_DEDATE, '%Y-%m-%d')
                firstdelidate_timestamp = datetime.strptime(data['firstDeliDate'], '%Y-%m-%d') + timedelta(hours=16) if 'firstDeliDate' in data else datetime.strptime(DEFAULT_DEDATE, '%Y-%m-%d')
                lastdelidate_timestamp = datetime.strptime(data['lastDeliDate'], '%Y-%m-%d') + timedelta(hours=16) if 'lastDeliDate' in data else datetime.strptime(DEFAULT_DEDATE, '%Y-%m-%d')
                print(data['ticker'])
                session.execute(insert_found, (data['ticker'], listdate_timestamp, lasttradedate_timestamp, firstdelidate_timestamp, lastdelidate_timestamp, data['secID'] if 'secID' in data else 'unknown', data['secFullName'] if 'secFullName' in data else 'unknown', data['secShortName'] if 'secShortName' in data else 'unknown', data['exchangeCD'] if 'exchangeCD' in data else 'unknown', data['contMultNum'] if 'contMultNum' in data else 0.0, data['contMultUnit'] if 'contMultUnit' in data else 'unknown', data['contractObject'] if 'contractObject' in data else 'unknown', data['contractStatus'] if 'contractStatus' in data else 'unknown', data['contractType'] if 'contractType' in data else 'unknown', data['deliCommiNum'] if 'deliCommiNum' in data else 0.0, data['deliCommiUnit'] if 'deliCommiUnit' in data else 'unknown', data['deliGrade'] if 'deliGrade' in data else 'unknown', data['deliMethod'] if 'deliMethod' in data else 'unknown', data['deliMonth'] if 'deliMonth' in data else 0, data['deliPriceMethod'] if 'deliPriceMethod' in data else 'unknown', data['deliYear'] if 'deliYear' in data else 0, data['limitDownNum'] if 'limitDownNum' in data else 0.0,  data['limitDownUnit'] if 'limitDownUnit' in data else 'unknown', data['limitUpNum'] if 'limitUpNum' in data else 0.0, data['limitUpUnit'] if 'limitUpUnit' in data else 'unknown', data['listBasisPrice'] if 'listBasisPrice' in data else 0.0, data['minChgPriceNum'] if 'minChgPriceNum' in data else 0.0, data['minChgPriceUnit'] if 'minChgPriceUnit' in data else 'unknown', data['priceUnit'] if 'priceUnit' in data else 'unknown', data['priceValidDecimal'] if 'priceValidDecimal' in data else 0, data['settPriceMethod'] if 'settPriceMethod' in data else 'unknown', data['tradeCommiNum'] if 'tradeCommiNum' in data else 0.0, data['tradeCommiUnit'] if 'tradeCommiUnit' in data else 'unknown', data['tradeMarginRatio'] if 'tradeMarginRatio' in data else 0.0, data['transCurrCD'] if 'transCurrCD' in data else 'unknown'))
            session.shutdown()
            cluster.shutdown()
    except Exception as e:
        print(e)

if __name__ == '__main__':
    client = Client('93ef5ebba60c5f1da190801750612423db818daad66155b6b191417b19d331b9')
    forward_exchange_code_list = ['XSGE', 'CCFX', 'XSGE', 'XDCE']
    for exchangeCD in forward_exchange_code_list:
        url = '/api/future/getFutu.json?field=&secID=&ticker=&contractObject=&exchangeCD=%s' % (exchangeCD)
        try:
            code, result = client.getData(url)
            if code == 200:
                get_forward_basic_info(result)
        except Exception as e:
            print(e)
